﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using 基于WPF的电缆采样数据分析系统.Models.Task;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using 基于WPF的电缆采样数据分析系统.Service;
using Microsoft.Win32;
using System.IO;
using Newtonsoft.Json;
using Prism.DryIoc;
using 基于WPF的电缆采样数据分析系统.Tools.MessagePopupTool;
using 基于WPF的电缆采样数据分析系统.Tools.LangugeTool;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork
{
    public class CreateProjectUCViewModel : BindableBase, IDialogHostAware
    {

        // 主机名
        private const string DialogHostName = "RootDialog";


        /// <summary>
        /// 构造器
        /// </summary>
        public CreateProjectUCViewModel()
        {
            // 绑定命令
            SaveCommand = new DelegateCommand(SaveFun);
            CancelCommand = new DelegateCommand(CancelFun); 
        }


        #region 确定

        /// <summary>
        /// 保存的文件路径
        /// </summary>
        public string SelectedFilePath { get; set; }

        /// <summary>
        /// 保存命令
        /// </summary>
        public DelegateCommand SaveCommand { get; set; }

        /// <summary>
        /// 保存方法
        /// </summary>
        private void SaveFun()
        {
            // 判断信息是否输入
            if (string.IsNullOrEmpty(ClientName))
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("CustomerNameIsNotFilledIn"));
                return;
            }

            if (string.IsNullOrEmpty(ProjectInfo))
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("EngineeringRemarksInformationIsNotEntered"));
                return;
            }

            // 获取输入信息
            var projectDataInfo = new ProjectDataInfo();
            projectDataInfo.ProjectInfo = ProjectInfo;
            projectDataInfo.ClientName = ClientName;
            projectDataInfo.CreateTime = DateTime.Now;
            projectDataInfo.UpdateTime = DateTime.Now;
            projectDataInfo.CreateUser = GlobalVariableEdit.CurrentUserInfo.Uid;
            projectDataInfo.UpdateUser = GlobalVariableEdit.CurrentUserInfo.Uid;

            // 工程默认新建一个任务
            List<TaskDataInfo> TaskList = new List<TaskDataInfo>();
            TaskList.Add(CreateNewTask());
            projectDataInfo.TaskList = TaskList;

            // 点击确定
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                RetResult retResult = SaveListFile(projectDataInfo);
                if (retResult == RetResult.Success)
                {
                    DialogParameters paras = new DialogParameters();
                    paras.Add("SelectedFilePath", SelectedFilePath);
                    DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, paras));
                } else if (retResult == RetResult.Fail)
                {
                    DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
                }
            }
        }

        /// <summary>
        /// 新建一个任务
        /// </summary>
        private TaskDataInfo CreateNewTask()
        {
            var XAxis = new ChannelArgument();
            XAxis.AxisOrigin = GlobalVariableEdit.XAxisStartPoint;
            XAxis.AxisEndPoint = GlobalVariableEdit.XAxisEndPoint;
            XAxis.AxisScale = GlobalVariableEdit.XAxisScale;
            var YAxis = new ChannelArgument();
            YAxis.AxisOrigin = GlobalVariableEdit.YAxisStartPoint;
            YAxis.AxisEndPoint = GlobalVariableEdit.YAxisEndPoint;
            YAxis.AxisScale = GlobalVariableEdit.YAxisScale;

            var taskDataInfo = new TaskDataInfo();
            taskDataInfo.Tid = 1;
            taskDataInfo.DescriptionInfo = LangugeConvertor.GetValue("RW_Nothing");
            taskDataInfo.ChannelConfig = GlobalVariableEdit.CurrentChannelIndex;
            taskDataInfo.HorizontalArgument = XAxis;
            taskDataInfo.VerticalArgument = YAxis;
            taskDataInfo.CreateTime = DateTime.Now;
            taskDataInfo.UpdateTime = DateTime.Now;
            taskDataInfo.XAxisTranslation = GlobalVariableEdit.XAxisTranslation;
            taskDataInfo.YAxisTranslation = GlobalVariableEdit.YAxisTranslation;
            taskDataInfo.CreateUser = GlobalVariableEdit.CurrentUserInfo.Uid;
            taskDataInfo.UpdateUser = GlobalVariableEdit.CurrentUserInfo.Uid;

            return taskDataInfo;
        }

        /// <summary>
        /// 保存工程为list文件
        /// </summary>
        /// <returns></returns>
        private RetResult SaveListFile(ProjectDataInfo projectDataInfo)
        {
            // 创建一个新的 SaveFileDialog 实例
            try
            {
                var saveFileDialog = new SaveFileDialog();
                // 设置对话框标题
                saveFileDialog.Title = LangugeConvertor.GetValue("SaveFile");
                // 过滤器
                saveFileDialog.Filter = $"LIST {LangugeConvertor.GetValue("File")} (*.list)|*.list";

                // 设置默认扩展名
                saveFileDialog.DefaultExt = "list";
                // 设置默认文件名
                saveFileDialog.FileName = LangugeConvertor.GetValue("ProjectFile") + DateTime.Now.ToString("yyyyMMddHHmmss");

                // 显示对话框并检查用户是否点击了“保存”
                if (saveFileDialog.ShowDialog() == true)
                {
                    // 用户选择了文件名并点击了“保存”按钮
                    string selectedFilePath = saveFileDialog.FileName;

                    string projectDataInfoJson = JsonConvert.SerializeObject(projectDataInfo);

                    // 将一些数据写入到选定的文件中
                    SelectedFilePath = selectedFilePath;
                    File.WriteAllText(selectedFilePath, projectDataInfoJson);
                    return RetResult.Success;
                }

                return RetResult.Fail;
            }
            catch (Exception ex)
            {
                MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("CauseOfError") + ex.Message);
                return RetResult.Fail;
            }
        }

        #endregion


        #region 取消

        /// <summary>
        /// 取消命令
        /// </summary>
        public DelegateCommand CancelCommand { get; set; }

        /// <summary>
        /// 取消方法
        /// </summary>
        private void CancelFun()
        {
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
            }
        }
        #endregion


        #region 页面信息

        /// <summary>
        /// 备注
        /// </summary>
        private string _ProjectInfo;

        /// <summary>
        /// 备注
        /// </summary>
        public string ProjectInfo
        {
            get { return _ProjectInfo; }
            set { 
                _ProjectInfo = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 客户
        /// </summary>
        private string _ClientName;

        /// <summary>
        /// 客户
        /// </summary>
        public string ClientName
        {
            get { return _ClientName; }
            set { 
                _ClientName = value;
                RaisePropertyChanged();
            }
        }


        #endregion


        public void OnDialogOpening(IDialogParameters parameters)
        {

        }
    }
    
}
